package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handlerSqlException1(SQLIntegrityConstraintViolationException ex) {
        String message = ex.getMessage();

            String username = message.split(" ")[2];
            return R.error(username + "该用户已存在");


    }

    @ExceptionHandler(CustomerException.class)
    public R<String> handlerSqlException2(CustomerException ex) {
        String message = ex.getMessage();


        return R.error(message);


    }
}
